home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / Libraries / VideoToolbox 97.08.16 / VideoToolboxSources / CenterRectInRect.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-02-23  |  2.3 KB  |  87 lines  |  [TEXT/CWIE]

  1. /*
  2. CenterRectInRect.c
  3. ©1991-1997 Denis G. Pelli
  4. These routines are trivial, but enhance the readability of programs that use them.
  5.  
  6. HISTORY:
  7. 8/24/91    dgp    Made compatible with THINK C 5.0.
  8. 1/25/93 dgp removed obsolete support for THINK C 4.
  9. 11/22/94 dgp renamed "RectInRect" to IsRectInRect().
  10. 5/31/95 dgp added ExpandRect, ExpandAndOffsetRect, and ShrinkRect.
  11. 6/17/95 dgp rewrote ShrinkRect to extend the rect to fully include any partial tiles.
  12. */
  13. #include "VideoToolbox.h"
  14.  
  15. void CenterRectInRect(Rect *a,Rect *b)
  16. {
  17.     OffsetRect(a,(b->left + b->right - a->left - a->right)/2
  18.         ,(b->top + b->bottom - a->top - a->bottom)/2);
  19. }
  20.  
  21. void PinRectInRect(Rect *a,Rect *b)
  22. {
  23.     if(a->right>b->right)OffsetRect(a,b->right-a->right,0);
  24.     if(a->bottom>b->bottom)OffsetRect(a,0,b->bottom-a->bottom);
  25.     if(a->left<b->left)OffsetRect(a,b->left-a->left,0);
  26.     if(a->top<b->top)OffsetRect(a,0,b->top-a->top);
  27. }
  28.  
  29. void OffsetRectTile(Rect *r,int nx,int ny)
  30. // Shift rect by multiples of itself
  31. {
  32.     OffsetRect(r,nx*(r->right-r->left),ny*(r->bottom-r->top));
  33. }
  34.  
  35. Boolean IsRectInRect(Rect *r,Rect *R)
  36. // Is the first rect entirely inside the second?
  37. // If either rect has zero area then this routine will return false.
  38. {
  39.     Rect t;
  40.     
  41.     if(!SectRect(r,R,&t))return 0;
  42.     return EqualRect(r,&t);
  43. }
  44.  
  45. void ShrinkRect(Rect *r,int hDivisor,int vDivisor)
  46. // Imagine that the plane is tiled by rects of size hDivisor by vDivisor.
  47. // We first extend your rect to fully include any tiles that were only partly included.
  48. // Then we shrink your rect by hDivisor and vDivisor.
  49. {
  50.     r->top=floor((float)r->top/vDivisor);
  51.     r->bottom=ceil((float)r->bottom/vDivisor);
  52.     r->left=floor((float)r->left/hDivisor);
  53.     r->right=ceil((float)r->right/hDivisor);
  54. }
  55.  
  56. void ExpandRect(Rect *r,double hMag,double vMag)
  57. {
  58.     r->top=round(r->top*vMag);
  59.     r->bottom=round(r->bottom*vMag);
  60.     r->left=round(r->left*hMag);
  61.     r->right=round(r->right*hMag);
  62. }
  63.  
  64. void ExpandAndOffsetRect(Rect *r,double hMag,double vMag,double hOffset,double vOffset)
  65. {
  66.     r->top=round(r->top*vMag+vOffset);
  67.     r->bottom=round(r->bottom*vMag+vOffset);
  68.     r->left=round(r->left*hMag+hOffset);
  69.     r->right=round(r->right*hMag+hOffset);
  70. }
  71.  
  72. void LocalToGlobalRect(Rect *r)
  73. {
  74.     Point pt={0,0};
  75.     
  76.     LocalToGlobal(&pt);
  77.     OffsetRect(r,pt.h,pt.v);
  78. }
  79.  
  80. void GlobalToLocalRect(Rect *r)
  81. {
  82.     Point pt={0,0};
  83.     
  84.     GlobalToLocal(&pt);
  85.     OffsetRect(r,pt.h,pt.v);
  86. }
  87.